package com.nervenets.web.controller.handler.admin.base;

import com.nervenets.web.controller.handler.SessionHandler;
import com.nervenets.web.controller.handler.common.UserPagingParams;
import com.nervenets.web.enumeration.Role;
import com.nervenets.web.hibernate.domain.user.Feedback;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class FeedbackListHandler extends SessionHandler<UserPagingParams> {
    @Override
    public ArrayList<Role> allowRoles() {
        return Role.administrator();
    }

    @Override
    public boolean needLog() {
        return false;
    }

    @Override
    public String handle(UserPagingParams params, HttpServletRequest request) throws Exception {
        String hql = "from Feedback ";
        HashMap<String, Object> objects = new HashMap<>();

        final BigDecimal countOfByHql = userDao.getCountOfByHql(hql, objects);
        hql += "order by id desc ";
        final List<Feedback> feedbacks = userDao.getAllObjectsByHql(hql, objects, params.getStart(), params.getLimit());

        return successMessage(feedbacks,
                new KeyValue("start", params.getStart()),
                new KeyValue("total", countOfByHql.intValue())
        );
    }

    @Override
    public String result() throws Exception {
        return successMessage("反馈列表");
    }
}
